package org.me.SendSMS.adapter;


import java.util.ArrayList;
import java.util.List;

import org.me.SendSMS.R;
import org.me.SendSMS.data.Model;
import org.me.SendSMS.utli.GlobalVariable;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class RepeatDateAdapter extends BaseAdapter {
	
	private LayoutInflater mInflater;
	private Context mContext;
	private List<Model> str;
	String s = "Monday";
	//String date_repeat = GlobalVariable.getValueKeyString(mContext, GlobalVariable.dateRepeat);
	List<String> arr = new ArrayList<String>();
	String string = "";
	
	
	public RepeatDateAdapter(Context context, List<Model> list)
	{
		mContext 	= context;
		//string 		= GlobalVariable.getValueKeyString(mContext, GlobalVariable.dateRepeat);
		mInflater 	= LayoutInflater.from(context);
		str 		= list;
       
	}
	
	public class ViewHolder
	{
		
		TextView txtRepeateDate;
		CheckBox cbCheckDate;
		
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return str.size();
}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		// TODO Auto-generated method stub

		final ViewHolder holder;

		if (view == null)
		{
			holder 					= new ViewHolder();
			view 					= mInflater.inflate(R.layout.repeat_date, null);
			
			holder.txtRepeateDate 	= (TextView)view.findViewById(R.id.txtRepeatDate);
			holder.cbCheckDate 		= (CheckBox)view.findViewById(R.id.cbCheckDate);
			
			view.setTag(holder);
		}
		
		else
		{
			holder = (ViewHolder) view.getTag();
		}

		
		holder.txtRepeateDate.setTag(position);
		holder.cbCheckDate.setTag(position);
		
		
		
		SharedPreferences myPrefs = mContext.getSharedPreferences("myPrefs", 1);
		String prefName = myPrefs.getString(str.get(position).getName() , "nothing");
	    if (prefName.equals(str.get(position).getName())) 
	    {
	       	Log.w("getView", "vo day may lan");
	       	holder.cbCheckDate.setChecked(true);
	    }
	    else
	    {
	       	Log.d("getView", "vo gi vo khan the");
	       	holder.cbCheckDate.setChecked(false);
	    }
		
		
		holder.cbCheckDate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				SharedPreferences myPrefs = mContext.getSharedPreferences("myPrefs", 2);
				SharedPreferences.Editor prefsEditor = myPrefs.edit();
				
				if(holder.cbCheckDate.isChecked())
				{
					prefsEditor.putString(str.get(position).getName(), str.get(position).getName());
					GlobalVariable.arrID.add(str.get(position).getPosition() + str.get(position).getName());
					Log.w("hung", "no vo day  = "+str.get(position).getName().substring(0, 3));
					
				}
				else
				{
					prefsEditor.putString(str.get(position).getName(), null);
					GlobalVariable.arrID.remove(str.get(position).getPosition() +str.get(position).getName());
					Log.w("hung", "hay la no no vo day = "+str.get(position).getName().substring(0, 3));
				}
				
			Log.w("oi cai dis", "cai gi the nay nhir = "+GlobalVariable.arrID);
			prefsEditor.commit();
			}
		});
		
		
		holder.txtRepeateDate.setText(str.get(position).getName());
		
		
		
		return view;
	}
	
	

}
